/*
 * @Author: zhangfeng
 * @Date: 2023-12-25 18:02:35
 * @LastEditTime: 2023-12-27 16:30:02
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \nodeapi\src\server\role.server.js
 * © 2023 张峰，All rights reserved.
 */
const Role = require("../model/role.model");
const { Op, where } = require("sequelize");

class RoleSrever {
  // 根据用户id查询对应的角色
  async getRoleServer(ids) {
    const result = await Role.findAll({
      where: {
        id: {
          [Op.in]: ids,
        },
      },
    });
    return result;
  }
  // 获取所有的角色
  async getRoleAllServer() {
    return await Role.findAll();
  }

  // 添加角色信息
  async createRoleServer(RoleData) {
    console.log(RoleData);
    const result = await Role.create(RoleData);
    return result.dataValues;
  }

  // 修改角色信息
  async updateRoleServer(RoleData) {
    const { id, name, description } = RoleData;
    const newRoles = {};
    name && Object.assign(newRoles, { name });
    description && Object.assign(newRoles, { description });
    const result = await Role.update(newRoles, {
      where: {
        id,
      },
    });
    return result[0] > 0;
  }

  // 删除角色信息
  async deleteRoleServer(id) {
    const result = await Role.destroy({
      where: {
        id,
      },
    });
    return result;
  }
}

module.exports = new RoleSrever();
